//生成菜单
var baseURL = "../../";
var menuItem = Vue.extend({
    name: 'menu-item',
    props: {item: {}},
    template: [
        '<li>',
        '	<a v-if="item.type === 0" href="javascript:;">',
        '		<i v-if="item.icon != null" :class="item.icon"></i>',
        '		<span>{{item.name}}</span>',
        '		<i class="fa fa-angle-left pull-right"></i>',
        '	</a>',
        '	<ul v-if="item.type === 0" class="treeview-menu">',
        '		<menu-item :item="item" v-for="item in item.list"></menu-item>',
        '	</ul>',

        '	<a v-if="item.type === 1 && item.parentId === 0" :href="\'#\'+item.url">',
        '		<i v-if="item.icon != null" :class="item.icon"></i>',
        '		<span>{{item.name}}</span>',
        '	</a>',

        '	<a v-if="item.type === 1 && item.parentId != 0" :href="\'#\'+item.url"><i v-if="item.icon != null" :class="item.icon"></i><i v-else class="fa fa-circle-o"></i> {{item.name}}</a>',
        '</li>'
    ].join('')
});

//iframe自适应
$(window).on('resize', function () {
    var $content = $('.content');
    $content.height($(this).height() - 120);
    $content.find('iframe').each(function () {
        $(this).height($content.height());
    });
}).resize();

//注册菜单组件
Vue.component('menuItem', menuItem);

var vm = new Vue({
    el: '#rrapp',
    data: {
        user: {},
        menuList: {},
        main: "modules/sys/tianangongshi.html",
        password: '',
        newPassword: '',
        navTitle: "提案公示"
    },
    methods: {
        getMenuList: function (event) {
            $.getJSON("sys/menu/nav?_" + $.now(), function (r) {
                vm.menuList = r.menuList;
            });
        },
        getUser: function () {
            $.getJSON("sys/user/info?_" + $.now(), function (r) {
                vm.user = r.user;
            });
        },
        updatePassword: function () {
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "修改密码",
                area: ['550px', '270px'],
                shadeClose: false,
                content: jQuery("#passwordLayer"),
                btn: ['修改', '取消'],
                btn1: function (index) {
                    var data = "password=" + vm.password + "&newPassword=" + vm.newPassword;
                    $.ajax({
                        type: "POST",
                        url: "sys/user/password",
                        data: data,
                        dataType: "json",
                        success: function (result) {
                            if (result.code == 0) {
                                layer.close(index);
                                layer.alert('修改成功', function (index) {
                                    location.reload();
                                });
                            } else {
                                layer.alert(result.msg);
                            }
                        }
                    });
                }
            });
        },

    },
    created: function () {
        this.getMenuList();
        this.getUser();
    },
    updated: function () {
        //路由
        var router = new Router();
        routerList(router, vm.menuList);
        router.start();
    }
});
$(function () {

    $(".shouye").click(function () {
$('iframe').attr("src",'/modules/sys/tianangongshi.html');
        window.location.hash="/modules/sys/tianangongshi.html";
    });
    function checks() {
        $.ajax({
            type: "get",
            url: baseURL + 'sys/tianan/listchulis',
            success: function (r) {
                if (r.page) {
                    if (r.page.list.length > 0) {

                        message(r.page.totalCount, "您部门有提案需要您处理", "立即处理", "#modules/sys/tiananchuli.html")
                    }
                }
            }
        });
    }


    function shenpi() {
        $.ajax({
            type: "get",
            url: baseURL + 'sys/tianan/listshenpi?banjiestatus=4',
            success: function (r) {
                if (r.page) {
                    if (r.page.list.length > 0) {
                        message(r.page.totalCount, "您有提案需要审批", "现在去审批", "#modules/sys/tiananshenpi.html")
                    }
                }
            }

        });
    }


    function yushen() {
        $.ajax({
            type: "get",
            url: baseURL + 'sys/tianan/listyushen?yushenstatus=0',
            success: function (r) {
                if (r.page) {
                    if (r.page.list.length > 0) {
                        message(r.page.totalCount, "您有提案需要预审", "立即预审", "#modules/sys/tiananyushen.html")
                    }
                }
            }

        });
    }

    function tuihui() {
        $.ajax({
            type: "get",
            url: baseURL + 'sys/tianan/listlian?lianstatus=3',
            success: function (r) {
                if (r.page) {
                    if (r.page.list.length > 0) {
                        message(r.page.totalCount, "您有提案被退回", "立即处理", "#modules/sys/tiananlian.html")
                    }
                }
            }

        });
    }
    function wode() {
        $.ajax({
            type: "get",
            url: baseURL + 'sys/tianan/list?banjiestatus=5',
            success: function (r) {
                if (r.page) {
                    if (r.page.list.length > 0) {
                        message(r.page.totalCount, "您的提案已办理，请评价", "去处理", "#modules/sys/tianan.html")
                    }
                }
            }

        });
    }

    wode();
    tuihui();
    yushen();
    shenpi();
    checks();
    // if (window.sessionStorage.getItem("tixing") != 1) {
    //
    //     layer.open({
    //         title: '系统提醒',
    //         type: 1,
    //         area: ['600px', '200px'],
    //         content:"<div style='padding:10px;line-height: 40px; '>提案系统当前处于内部测试阶段，待正式开放时，所有测试信息将会被清空，目前请勿提交正式提案！</div>" ,
    //         success: function () {
    //             $(".go").click(function () {
    //                 window.location.hash=url;
    //                 layer.closeAll();
    //             })
    //         },
    //     });
    //
    //     window.sessionStorage.setItem("tixing",1);
    //
    // }
    function message(total, title, content, url) {
        $(".xiaoxi").html(total);
        $(".xiaoxi").addClass("xiaoxiactive");
        $(".tishi").attr("title", title);
        $(".tishi").attr("data-content", "<a href='" + url + "'>" + content + "</a>");
        $("[data-toggle='popover']").popover();


        if (window.sessionStorage.getItem("once") != 1) {

                layer.open({
                    title: '消息提醒',
                    type: 1,
                    area: ['300px', '150px'],
                    content:"<div style='text-align: center;line-height: 40px;'>"+title+"<a class='go' href='javascript:;'>"+content+"</a></div>" ,
                    success: function () {
                        $(".go").click(function () {
                        window.location.hash=url;
                            layer.closeAll();
                        })
                    },
                });

                window.sessionStorage.setItem("once",1);

        }
    }

})

function routerList(router, menuList) {
    for (var key in menuList) {
        var menu = menuList[key];
        if (menu.type == 0) {
            routerList(router, menu.list);
        } else if (menu.type == 1) {
            router.add('#' + menu.url, function () {
                var url = window.location.hash;

                //替换iframe的url
                vm.main = url.replace('#', '');

                //导航菜单展开
                $(".sidebar-menu li").removeClass("active");
                $("a[href='" + url + "']").parents("li").addClass("active");

                vm.navTitle = $("a[href='" + url + "']").text();
            });
        }
    }
}
